//
//  MFWHttpEngine.h
//  MFWSdk
//
//  Created by Song Xiaofeng on 13-7-24.
//  Copyright (c) 2013年 Mafengwo Inc. All rights reserved.
//

#import <Foundation/Foundation.h>
extern NSString *API_HOST;

extern NSString *const MFWErrorDomain;
extern NSString *const kMFWErrorInfo_Str_Msg;// key in http error info dictionay -> error msg
extern NSString *const kMFWErrorInfo_Int_StatusCode;// key in http error info dictionary -> http response status code

typedef enum
{
    kError_UnknowError = -10000,
    kError_JsonParseFail = -10001,
    kError_NetworkTimeout = -10002,
    kError_ResultOk = 1,
    
}MFWErrorCode;

typedef enum
{
    kMFWDataRequest_Refresh = 0,
    kMFWDataREquest_GetMore = 1,
}MFWDataRequestType;

#ifdef DEBUG
//#define ASI_DEBUG_LOG   DLog
//#define DEBUG_REQUEST_STATUS    1
//#define DEBUG_FORM_DATA_REQUEST 1
#endif

@class MFWTask;
@class MFWDataModel;
@class MFWHttpTask;
@class MFWJson;
@class MFWHttpRequest;
@protocol MFWDataObserver <NSObject>

@optional
- (void)requestFinishedForTask:(MFWTask*)task withModel:(MFWDataModel*)model dataId:(NSString*)dataId requestType:(MFWDataRequestType)type itemArray:(NSArray*)itemArray success:(BOOL)bSuccess error:(NSError*)error;

- (void)requestFinishedForTask:(MFWTask*)task success:(BOOL)bSuccess error:(NSError*)error;
//- (void)requestDataModifyForTask:(MFWHttpTask*)task;
- (void)taskDidAddedToEngine:(MFWTask*)task;
@end


@protocol MFWGlobalHttpResultHook <NSObject>

@optional
/**
 *	Only when received data successfully, this callback will be called
 *
 *	@param	json	json data
 *	@param	mfwRequest	request
 */
- (void)globalJsonResultHook:(MFWJson*)json forRequest:(MFWHttpRequest*)mfwRequest;
- (void)globalRawResultHook:(NSData*)data forRequest:(MFWHttpRequest*)mfwRequest;
@end

@protocol MFWHttpTaskCallback <NSObject>

- (BOOL)processSuccRawData:(NSData*)rawData parsedJson:(MFWJson*)jsonObj forTask:(MFWHttpTask*)task;
- (void)processFailRawData:(NSData*)rawData parsedJson:(MFWJson*)jsonObj forTask:(MFWHttpTask*)task;

@end

typedef void(^MFWHttpRequestFilter) (MFWHttpRequest *mfwHttpRequest);

typedef enum
{
    kMFWRequestQueueTypeNormal = 0, // 普通请求队列
    kMFWRequestQueueTypeUpload,     // 普通上传队列
    kMFWRequestQueueTypeFileUpload, // 文件上传队列，会报告详细进度，执行相关回调
    
    kMFWRequestQueueCount,
}MFWRequestQueueType;



@interface MFWHttpEngine : NSObject
{
    // key-> (NSString)category name
    // value-> (NSArray) observer array, observer is referenced by which array weakly
    NSMutableDictionary *_weakDataObserverDict;
    
    // weak referenced array
    NSMutableArray      *_weakGlobalResultHooks;
    
    NSMutableArray      *_networkQueues;
}

/**
 *	every MFWHttpRequest which will be added to HttpEngine should filtered by this filter
 */
@property(nonatomic, copy)MFWHttpRequestFilter requestFilter;

+ (MFWHttpEngine*)sharedInstance;

+ (void)setApiHost:(NSString*)apiHost;
/**
 *	设置全局的request filter
 *  可以利用改filter, 在被add到HttpEngine前，对该request做修改，一般只做参数的处理（添加公共参数等）
 *
 *	@param	requestFilter	filter block
 */
+ (void)setMFWHttpRequestFilter:(MFWHttpRequestFilter)requestFilter;

- (BOOL)addTask:(MFWHttpTask*)task;
- (BOOL)addRequest:(MFWHttpRequest*)request;

- (void)cancelHttpRequest:(MFWHttpRequest*)request;
- (void)cancelTask:(MFWHttpTask*)task;
- (void)cancelAllTaskBySender:(id)sender;

/**
 *	add weak data observer, observer will be referenced weakly
 *
 *	@param	anObserver	data observer
 *	@param	category	data category
 */
- (void)addDataObserver:(id<MFWDataObserver>)anObserver forDataCategory:(NSString*)category;


/**
 *	remove data observer which obsering on the category
 *
 *	@param	anObserver	data observer
 *	@param	category	data category
 */
- (void)removeDataObserver:(id<MFWDataObserver>)anObserver forDataCategory:(NSString*)category;

/**
 *	remove data oberver which obsering on all category
 *
 *	@param	anObserver	data observer
 */
- (void)removeDataObserver:(id<MFWDataObserver>)anObserver;

/**
 *	add global http result hook. This hook will not be retained.
 *
 *	@param	anResultHook	global hook
 */
- (void)addGlobalHttpResultHook:(id<MFWGlobalHttpResultHook>)anResultHook;
- (void)removeGlobalHttpResultHook:(id<MFWGlobalHttpResultHook>)anResultHook;

- (void)cancelHttpQueue:(MFWRequestQueueType)queuetype;
- (void)cancelAllHttpQueue;

@end
